package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>  implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;


    /**
     * 增加
     * @param setmealDto
     */
    @Transactional
    public void addSetmeal(SetmealDto setmealDto) {

        //1、保存套餐数据到 setmeal表中 ： SetmealMapper
        this.save(setmealDto);

//2、保存套餐菜品数据到 setmeal_dish表中： SetmealDishMapper
//2.1 获取当前套餐的菜品列表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish meal : setmealDishes) {
            meal.setSetmealId(setmealDto.getId());
        }



//2.2 循环遍历，获取每一个套餐菜品数据 setmealDish

        //2.3 获取套餐id，并set到setmealDish中


        //2.4 保存套餐菜品数据 setmealDish
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 修改状态
     * @param status
     * @param ids
     */
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        List<Setmeal> list=new ArrayList<Setmeal>() ;
        for (Long id : ids) {
            Setmeal se = setmealService.getById(id);
            se.setStatus(status);
            list.add(se);

        }
        setmealService.updateBatchById(list);


    }

    /**
     * 删除
     * @param ids
     */
    @Transactional
    public void removeWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1);

        if(this.count(qw)>0)
            throw new CustomException("套餐正在售卖中，不能删除");

        this.removeByIds(ids);
//3、删除套餐对应的菜品数据：delete from setmeal_dish where setmeal_id in (1,2,3)

        LambdaQueryWrapper<SetmealDish> qw2=new LambdaQueryWrapper<>();
        qw2.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(qw2);
    }

    /**
     * 根据套餐id查询套餐数据
     * @param id
     * @return
     */
    @Override
    public SetmealDto getSetmealById(Long id) {
        SetmealDto setmealDto=new SetmealDto();
        Setmeal setmeal = setmealService.getById(id);
        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> qw=new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(qw);
        setmealDto.setSetmealDishes(list);
        return setmealDto;

    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        Long id = setmealDto.getId();

        LambdaQueryWrapper<SetmealDish> qw=new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        //删除菜品
        setmealDishService.remove(qw);

        //添加菜品
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        list=list.stream().map((item)->{
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(list);

    }
}
